package com.cd.javacxy.module.web.service.impl;

import com.cd.javacxy.common.model.entity.CxyUserFriend;
import com.cd.javacxy.common.model.enums.PageSize;
import com.cd.javacxy.common.model.query.UserFriendQuery;
import com.cd.javacxy.common.model.vo.CxyUserVo;
import com.cd.javacxy.common.utils.Page;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.web.dao.CxyUserFriendDao;
import com.cd.javacxy.module.web.service.CxyUserFriendService;
import com.cd.javacxy.module.web.service.CxyUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;

/**
 * CxyUserFriend的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxyUserFriendServiceImpl implements CxyUserFriendService {


    private final CxyUserFriendDao cxyUserFriendDao;
    private final CxyUserService cxyUserService;


    @Override
    public int findFocusType4UserHome(UserFriendQuery userFriendQuery) {
        if (userFriendQuery.getUserId() == null) {
            return 1;
        }
        if (userFriendQuery.getUserId().intValue() == userFriendQuery.getFriendUserId().intValue()) {
            return 0;
        }
        List<CxyUserFriend> list = this.cxyUserFriendDao.selectList(userFriendQuery);
        if (list.isEmpty()) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int findCount(UserFriendQuery userFriendQuery) {
        if (userFriendQuery.getFriendUserId() == null && userFriendQuery.getUserId() == null) {
            return 0;
        }
        return this.cxyUserFriendDao.selectCount(userFriendQuery);
    }

    @Override
    public PageResult<CxyUserFriend> findFriendList(
            UserFriendQuery userFriendQuery) {
        int count = this.cxyUserFriendDao.selectCount(userFriendQuery);
        int size = PageSize.PAGE_SIZE21.getSize();
        int pageNum = 1;
        if (userFriendQuery.getPageNum() != 1) {
            pageNum = userFriendQuery.getPageNum();
        }
        Page page = new Page(pageNum, count, size);
        userFriendQuery.setPage(page);
        List<CxyUserFriend> list = this.cxyUserFriendDao.selectList(userFriendQuery);
        return new PageResult<>(page, list);
    }

    @Override
    public PageResult<CxyUserFriend> findFansList(UserFriendQuery userFriendQuery) {
        int count = this.cxyUserFriendDao.selectCount(userFriendQuery);
        int size = PageSize.PAGE_SIZE21.getSize();
        int pageNum = 1;
        if (userFriendQuery.getPageNum() != 1) {
            pageNum = userFriendQuery.getPageNum();
        }
        Page page = new Page(pageNum, count, size);
        userFriendQuery.setPage(page);
        List<CxyUserFriend> list = this.cxyUserFriendDao.selectList(userFriendQuery);
        return new PageResult<>(page, list);
    }


    @Override
    public int addFocus(CxyUserFriend userFriend) throws BussinessException {
        if (userFriend.getFriendUserId() == null || userFriend.getUserId() == null) {
            throw new BussinessException("参数错误");
        }
        CxyUserVo friendUser = this.cxyUserService.findUserByUserId(userFriend.getFriendUserId());
        userFriend.setFriendUserName(friendUser.getUserName());
        userFriend.setFriendUserIcon(friendUser.getUserIcon());
        userFriend.setCreateTime(Instant.now());
        this.cxyUserFriendDao.insert(userFriend);
        return 0;
    }

    @Override
    public int cancelFocus(CxyUserFriend userFriend) throws BussinessException {
        if (userFriend.getFriendUserId() == null || userFriend.getUserId() == null) {
            throw new BussinessException("参数错误");
        }
        this.cxyUserFriendDao.delete(userFriend);
        return 0;
    }

}